package com.shujia

object Demo3Base {
  def main(args: Array[String]): Unit = {

    /**
     * 算术运算
     */
    val i = 100
    val j = 3

    println(i + j)
    println(i - j)
    println(i * j)
    println(i / j)
    println(i % j)

    /**
     * 关系运算符
     */

    println(i == j)
    println(i != j)
    println(i >= j)
    println(i <= j)
    println(i > j)
    println(i < j)


    /**
     * 逻辑运算符
     */
    println(true && false)
    println(true || false)
    println(!true)

    /**
     * 赋值运算符
     */
    var a = 100
    a = a + 1
    a += 1
    println(a)

    a -= 1
    println(a)

    a *= 2
    println(a)

    var b = 2
    //移位运算
    b <<= 1

    println(b)

    /**
     * scala运算符的本质
     */
    var age = 23

    //调用方法
    age = age.+(2)

    //方法调用的一种简化（可以省略括号和点号）
    age = age + 2
    println(age)

    val str = "java,spark"
    //简写
    val strings: Array[String] = str split ","


    /**
     * if语句
     *
     */
    val name = "张三"
    if (name == "张三") {
      println("姓名是张三")
    } else if (name == "零四") {
      println("姓名是里斯")
    } else {
      println("其他")
    }

    /**
     * 增强fow循环
     */

    // 1.to(10) : 等到一个序列
    println(1.to(10))

    //使用增强for循环，循环序列
    for (i <- 1 to 10) {
      println(i)
    }

    val str1 = "java,spark,hadoop,hive"
    val words: Array[String] = str1.split(",")

    //使用for循环循环数组
    for (word: String <- words) {
      println(word)
    }


    /**
     * while循环
     */
    var k = 1
    var sum = 1
    while (k <= 10) {
      sum *= k
      k += 1
    }
    println(sum)


    /**
     * 冒泡排序
     */
    //Array相当于java中的数组
    val ints: Array[Int] = Array(123, 123, 12, 312, 4, 123, 1, 412, 3, 124, 12, 3, 123)

    //until: 含头不含尾
    for (i <- 0 until ints.length - 1) {
      for (j <- 0 until ints.length - 1 - i) {
        if (ints(j) > ints(j + 1)) {
          val tmp: Int = ints(j)
          ints(j) = ints(j + 1)
          ints(j + 1) = tmp
        }
      }
    }
    //数组直接打印结果是内存地址
    //toList:转换成集合
    println(ints.toList)

  }
}
